﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;


namespace TSQLPARSER
{

    sealed partial class xParser
    {
        private void SetTableType(string tblType)
        {
            _CurrentStatement.xTableList.ForEach(delegate(xTable tbl) { if (tbl.Tbltype == null)tbl.Tbltype = tblType; });
        }

        /// <summary>
        /// Adds the xStatement to the list and then creates a new xStatement to process.
        /// </summary>
        /// <param name="xstm"></param>
        /// <param name="xtf"></param>
        private void AddtoList(xStatement xstm, TSqlFragment xtf)
        {
            if (xtf != null)
                xstm.SqlSnippet = GetSQLSnippet(xtf.StartOffset, xtf.FragmentLength);
            xStmtlist.Add(xstm);
            Allstmts[Currentstmt].Add(xstm);
            _CurrentStatement = new xStatement() { xColumnList = new List<xColumn>(), xTableList = new List<xTable>() };
        }
        private void ProcessCast(CastCall castCall)
        {
            Expression E = castCall.Parameter as Expression;
            {
                ProcessTSqlFragment(E);
            }
        }

        private void ProcessCovert(ConvertCall convertCall)
        {
            Expression E = convertCall.Parameter as Expression;
            {
                ProcessTSqlFragment(E);
            }
        }

        private void ProcessFunction(FunctionCall fn)
        {
            foreach (Expression E in fn.Parameters)
            {
                ProcessTSqlFragment(E);
            }
        }


        //private void ProcessParameters(Expression E)
        //{
        //    ProcessTSqlFragment(E);
        //    //if (E is Column) GetColumns(E as Column);
        //    //else if (E is FunctionCall) ProcessFunction(E as FunctionCall);
        //    //else if (E is ConvertCall) ProcessCovert(E as ConvertCall);
        //    //else if (E is CastCall) ProcessCast(E as CastCall);
        //    //else if (E is Subquery) ProcessSelect((E as Subquery).QueryExpression);
        //    //else if (E is ParenthesisExpression) ProcessTSqlFragment((E as ParenthesisExpression).Expression);

        //}

        private void GetColumns(Column c)
        {

            string WildCard=string.Empty;
            int count = c.Identifiers.Count;

            if (c.ColumnType == ColumnType.Wildcard)
            {
                _CurrentStatement.xColumnList.Add(new xColumn() { Column = "*", TableAlias = c.Identifiers.Count == 0 ? "" : c.Identifiers[0].Value.ToUpper() });
            }
            else
            {
                if (count == 0) return;
                _CurrentStatement.xColumnList.Add(new xColumn() { Column = c.Identifiers[count - 1].Value, TableAlias = c.Identifiers.Count == 1 ? "" : c.Identifiers[0].Value.ToUpper() });
            }
            

        }

        private void GetIndetifiers(Identifier c)
        {

            _CurrentStatement.xColumnList.Add(new xColumn() { Column = c.Value });
        }

        /// <summary>
        /// Gets Snippet of the TSQL statement associated with parsed output
        /// </summary>
        /// <param name="start"></param>
        /// <param name="Length"></param>
        /// <returns></returns>
        private string GetSQLSnippet(int start, int Length)
        {
            return FileText.Substring(start, Length);
        }
    }
}
